#!/bin/bash -eu
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################

# Fuzzer runner. Appends .options arguments and seed corpus to users args.
# Usage: $0 <fuzzer_name> <fuzzer_args>

export PATH=$OUT:$PATH
cd $OUT

FUZZER=$1
shift

CORPUS_DIR="/tmp/${FUZZER}_corpus"

if [[ "$RUN_FUZZER_MODE" = interactive ]]; then
  FUZZER_OUT="$OUT/${FUZZER}_${FUZZING_ENGINE}_${SANITIZER}_out"
else
  FUZZER_OUT="/tmp/${FUZZER}_${FUZZING_ENGINE}_${SANITIZER}_out"
fi

function get_dictionary() {
  local options_file="$FUZZER.options"
  local dict_file="$FUZZER.dict"
  local dict=""
  if [[ -f "$options_file" ]]; then
    dict=$(sed -n 's/^\s*dict\s*=\s*\(.*\)/\1/p' "$options_file" | tail -1)
  fi
  if [[ -z "$dict" && -f "$dict_file" ]]; then
    dict="$dict_file"
  fi
  [[ -z "$dict" ]] && return
  if [[ "$FUZZING_ENGINE" = "libfuzzer" ]]; then
     printf -- "-dict=%s" "$dict"
  elif [[ "$FUZZING_ENGINE" = "afl" ]]; then
     printf -- "-x %s" "$dict"
  elif [[ "$FUZZING_ENGINE" = "honggfuzz" ]]; then
     printf -- "--dict %s" "$dict"
  else
     printf "Unexpected FUZZING_ENGINE: $FUZZING_ENGINE, ignoring\n" >&2
  fi
}

rm -rf $CORPUS_DIR && mkdir $CORPUS_DIR
rm -rf $FUZZER_OUT && mkdir $FUZZER_OUT

SEED_CORPUS="${FUZZER}_seed_corpus.zip"
if [ -f $SEED_CORPUS ] && [ -z ${SKIP_SEED_CORPUS:-} ]; then
  echo "Using seed corpus: $SEED_CORPUS"
  unzip -d ${CORPUS_DIR}/ $SEED_CORPUS > /dev/null
fi

if [[ "$FUZZING_ENGINE" = afl ]]; then
  # https://chromium.googlesource.com/chromium/src/+/master/third_party/afl/src/docs/env_variables.txt
  export ASAN_OPTIONS="$ASAN_OPTIONS:abort_on_error=1:symbolize=0"
  export MSAN_OPTIONS="$MSAN_OPTIONS:exit_code=86:symbolize=0"
  export UBSAN_OPTIONS="$UBSAN_OPTIONS:symbolize=0"
  export AFL_I_DONT_CARE_ABOUT_MISSING_CRASHES=1
  export AFL_SKIP_CPUFREQ=1
  # AFL expects at least 1 file in the input dir.
  echo input > ${CORPUS_DIR}/input
  CMD_LINE="$OUT/afl-fuzz $AFL_FUZZER_ARGS -i $CORPUS_DIR -o $FUZZER_OUT $(get_dictionary) $* $OUT/$FUZZER"
elif [[ "$FUZZING_ENGINE" = honggfuzz ]]; then
  # Honggfuzz expects at least 1 file in the input dir.
  echo input > $CORPUS_DIR/input
  # --exit_upon_crash: exit whith a first crash seen
  # -R (report): save report file to this location
  # -W (working dir): where the crashes go
  # -v (verbose): don't use VTE UI, just stderr
  # -z: use software-instrumentation of clang (trace-pc-guard....)
  # -P: use persistent mode of fuzzing (i.e. LLVMFuzzerTestOneInput)
  # -f: location of the initial (and destination) file corpus
  # -n: number of fuzzing threads (and processes)
  CMD_LINE="$OUT/honggfuzz -n 1 --exit_upon_crash -R /tmp/${FUZZER}_honggfuzz.report -W $FUZZER_OUT -v -z -P -f \"$CORPUS_DIR\" $(get_dictionary) $* -- \"$OUT/$FUZZER\""
else
  CMD_LINE="$OUT/$FUZZER $FUZZER_ARGS $* $CORPUS_DIR"

  OPTIONS_FILE="${FUZZER}.options"
  if [ -f $OPTIONS_FILE ]; then
    OPTIONS_ARGS=$(grep "=" $OPTIONS_FILE | sed 's/\(\w*\)\W*=\W*\(.*\)/-\1=\2 /g' | tr '\n' ' ')
    CMD_LINE="$CMD_LINE $OPTIONS_ARGS"
  fi

  if [[ ! "$CMD_LINE" =~ "-dict=" ]]; then
    if [ -f "$FUZZER.dict" ]; then
      CMD_LINE="$CMD_LINE -dict=$FUZZER.dict"
    fi
  fi

  CMD_LINE="$CMD_LINE < /dev/null"
fi

echo $CMD_LINE
bash -c "$CMD_LINE"
